﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LiquidTrading.LiquidViewSyncTool.Properties;
using System.Net;
using Algobox.JuicyLib.SQL.LiquidView;
using System.IO;
using Algobox.JuicyLib.MarketData.Structure;
using Algobox.JuicyLib.Apps.Yahoo;
using System.Collections;

namespace LiquidTrading.LiquidViewSyncTool
{
    class Program
    {
        static void Main(string[] args)
        {
            _dbLinq = new LiquidViewLinqDataContext(Settings.Default.DBConString);
            LiquidViewHelper.LiquidViewLinq = _dbLinq;

            try
            {
                Console.WriteLine("---- START BATE ----");
                DoBATE();
                Console.WriteLine("---- END   BATE ----");
            }
            catch (Exception e)
            {
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing BATE files. Exception: " + e.Message);
            }
            try
            {
                Console.WriteLine("---- START CHIX ----");
                DoCHIX();
                Console.WriteLine("---- END   CHIX ----");
            }
            catch (Exception e)
            {
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing CHIX files. Exception: " + e.Message);
            }
            try
            {
                Console.WriteLine("---- START TRQX ----");
                DoTRQX();
                Console.WriteLine("---- END   TRQX ----");
            }
            catch (Exception e)
            {
                LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing TRQX files. Exception: " + e.Message);
            }


            //// prices from Yahoo
            //try
            //{
            //    Console.WriteLine("---- START Yahoo ----");
            //    Yahoo.ProcessYahoo();
            //    Console.WriteLine("---- END   Yahoo ----");
            //}
            //catch (Exception e)
            //{
            //    LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing Yahoo! Exception: " + e.Message);
            //}
            
            //// US Names
            //try
            //{
            //    Console.WriteLine("---- START   US ----");
            //    EODData.DoUS();
            //    Console.WriteLine("---- END     US ----");
            //}
            //catch (Exception e)
            //{
            //    LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing US EOD files. Exception: " + e.Message);
            //}

            //// CANADA Names
            //try
            //{
            //    Console.WriteLine("---- START  CAD ----");
            //    EODData.DoCanada();
            //    Console.WriteLine("---- END    CAD ----");
            //}
            //catch (Exception e)
            //{
            //    LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing EOD Canada files. Exception: " + e.Message);
            //}

            //// import prices
            //try
            //{
            //    Console.WriteLine("---- START EOD ----");
            //    EODData.DoEOD();
            //    Console.WriteLine("----  END EOD  ----");
            //}
            //catch (Exception e)
            //{
            //    LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing EOD files. Exception: " + e.Message);
            //}

            //// import splits
            //try
            //{
            //    Console.WriteLine("---- START Splits ----");
            //    EODData.DoSplits();
            //    Console.WriteLine("----  END Splits  ----");
            //}
            //catch (Exception e)
            //{
            //    LiquidViewHelper.LogException("LiquidViewSyncTool", "Caught an excpetion processing EOD files. Exception: " + e.Message);
            //}
        }

        static LiquidViewLinqDataContext _dbLinq;
        static WebClient _webClient = new WebClient();
        
        public const int MAX_RETRIES = 3;
        public static LiquidViewLinqDataContext DBLinq { get { return _dbLinq; } }
       


        public static string CompareTheMeerkat(string meerkat)
        {
            if (meerkat == "MTAA")
                return "XMIL";
            else if (meerkat == "XVTX")
                return "XSWX";
            else if (meerkat == "XWBO")
                return "WBAH";

            return meerkat;
        }


        /// <summary>
        /// BATS
        /// </summary>
        private static void DoBATE()
        {
            byte[] bytes;
            string data;

            if (Settings.Default.FILE_BATE_SYMBOL != String.Empty
                && Settings.Default.FILE_BATE_TICKS != String.Empty)
            {
                try
                {
                    bytes = _webClient.DownloadData(Settings.Default.FILE_BATE_TICKS);
                    if (bytes.Length > 0)
                    {
                        data = new ASCIIEncoding().GetString(bytes);
                        ProcessBats.ProcessTicks(_dbLinq, ref data);

                        Console.WriteLine("Processed Ticks");

                        try
                        {
                            bytes = _webClient.DownloadData(Settings.Default.FILE_BATE_SYMBOL);
                            if (bytes.Length > 0)
                            {
                                data = new ASCIIEncoding().GetString(bytes);
                                ProcessBats.ProcessSymbols(_dbLinq, ref data);
                                Console.WriteLine("Processed Symbols");
                            }
                            else
                            {
                                Console.WriteLine("Failed to process Symbols");
                                _dbLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "Bats - Symbols couldn't load " + Path.GetFileName(Settings.Default.FILE_BATE_SYMBOL), null);
                            }
                        }
                        catch (WebException e)
                        {
                            Console.WriteLine(e.Message + " for file " + Settings.Default.FILE_BATE_SYMBOL);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to process Ticks");
                        _dbLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "Bats - Ticks couldn't load " + Path.GetFileName(Settings.Default.FILE_BATE_TICKS), null);
                    }
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.Message + " for file " + Settings.Default.FILE_BATE_TICKS);
                }
            }
            else
            {
                Console.WriteLine("BATE settings not found");
            }
        }



        /// <summary>
        /// CHIX
        /// </summary>
        private static void DoCHIX()
        {
            byte[] bytes;
            string data;

            if (Settings.Default.FILE_CHIX_SYMBOLS != String.Empty
                && Settings.Default.FILE_CHIX_TICKS != String.Empty)
            {
                try
                {
                    bytes = _webClient.DownloadData(Settings.Default.FILE_CHIX_TICKS);
                    if (bytes.Length > 0)
                    {
                        data = new ASCIIEncoding().GetString(bytes);
                        ProcessChix.ProcessTicks(_dbLinq, ref data);

                        Console.WriteLine("Processed Ticks");

                        try
                        {
                            bytes = _webClient.DownloadData(Settings.Default.FILE_CHIX_SYMBOLS);
                            if (bytes.Length > 0)
                            {
                                data = new ASCIIEncoding().GetString(bytes);
                                ProcessChix.ProcessSymbols(_dbLinq, ref data);
                                Console.WriteLine("Processed Symbols");
                            }
                            else
                            {
                                Console.WriteLine("Failed to process Symbols");
                                _dbLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "Chi-X - Symbols couldn't load " + Path.GetFileName(Settings.Default.FILE_CHIX_SYMBOLS), null);
                            }
                        }
                        catch (WebException e)
                        {
                            Console.WriteLine(e.Message + " for file " + Settings.Default.FILE_CHIX_SYMBOLS);

                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to process Ticks");
                        _dbLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "Chi-X - Ticks couldn't load " + Path.GetFileName(Settings.Default.FILE_CHIX_TICKS), null);
                    }
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.Message + " for file " + Settings.Default.FILE_CHIX_TICKS);

                }
            }
            else
            {
                Console.WriteLine("CHIX settings not found");
            }
        }


        /// <summary>
        /// Turquoise
        /// </summary>
        private static void DoTRQX()
        {
            byte[] bytes;

            if (Settings.Default.FILE_TRQX_SYMBOLS != String.Empty
                && Settings.Default.FILE_TRQX_TICKS != String.Empty)
            {
                string symbolsLocation = Settings.Default.FILE_TRQX_SYMBOLS.Replace("YYYYMMDD", DateTime.Today.ToString("yyyyMMdd"));
                string ticksLocation = Settings.Default.FILE_TRQX_TICKS.Replace("YYYYMMDD", DateTime.Today.ToString("yyyyMMdd"));

                try
                {
                    bytes = _webClient.DownloadData(ticksLocation);
                    if (bytes.Length > 0)
                    {
                        ProcessTrqx.ProcessTicks(_dbLinq, bytes);

                        Console.WriteLine("Processed Ticks");

                        try
                        {
                            bytes = _webClient.DownloadData(symbolsLocation);
                            if (bytes.Length > 0)
                            {
                                ProcessTrqx.ProcessSymbols(_dbLinq, bytes);

                                Console.WriteLine("Processed Symbols");
                            }
                            else
                            {
                                Console.WriteLine("Failed to process Symbols");
                                _dbLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "Turquoiose - Symbols couldn't load " + Path.GetFileName(symbolsLocation), null);
                            }
                        }
                        catch (WebException e)
                        {
                            Console.WriteLine(e.Message + " for file " + symbolsLocation);

                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to process Ticks");
                        _dbLinq.ExceptionHandlerThrow("LiquidViewSyncTool", "Turquoiose - Ticks couldn't load " + Path.GetFileName(ticksLocation), null);
                    }
                }
                catch (WebException e)
                {
                    Console.WriteLine(e.Message + " for file " + ticksLocation);

                }
            }
            else
            {
                Console.WriteLine("TRQX settings not found");
            }
        }
    }
}
